/**
 * 结束界面
 */
class FinishScene extends eui.Component {

    lab_roomid: eui.Label;
    lab_rule: eui.Label;
    btn_close: ButtonEx;

    itemGroup: eui.Group;

    lab_game: eui.Label;
    lab_time: eui.Label;

    btn_share: ButtonEx;
    btn_assess: eui.Button;

    private roleList: Array<any> = [];//玩家列表
    private fenList: Array<any> = [];//分数列表

    private data: any;

    constructor() {
        super();

        this.skinName = "FinishSceneSkin";
    }

    childrenCreated() {
        super.childrenCreated();

        this.top = this.right = this.bottom = this.left = 0;

        this.lab_game.text = `【${GameConfig.mahjong_name}】`;

        if (GameConfig.isPureness) this.btn_share.visible = false;

        this.btn_close.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
        this.btn_share.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
        this.btn_assess.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
    }

    private clickHandler(e: egret.TouchEvent) {
        switch (e.currentTarget) {
            case this.btn_assess:
                let changePanel: AssessChangePanel = gameManager.find('AssessChangePanel');
                if (changePanel) {
                    let data: any = {};
                    let obj: any = {};
                    obj.data = data;
                    let person: any;
                    for (let i: number = 0; i < this.data.persons.length; i++) {
                        if (this.data.persons[i].uid != game.player.uid) {
                            person = {};
                            person.uid = this.data.persons[i].uid;
                            person.pic = this.data.persons[i].pic;
                            person.nick = this.data.persons[i].nick;
                            person.zan = 1;

                            data[person.uid] = person;
                        }
                        else {
                            obj.id = this.data.persons[i].appraise_id;
                        }
                    }
                    changePanel.show();
                    changePanel.update(obj);

                    game.isShowAsscess = false;
                }
                break;
            case this.btn_close:
                this.onClose();
                break;
            case this.btn_share:
                game.share.show();
                Weixin.shareScore(this.fenList);
                break;
        }
    }

    onClose() {
        if (this.parent && this.parent.contains(this)) {
            this.parent.removeChild(this);
            GSController.i.jiesuanData = null;
            GSController.i.exit();
        }
        if (game.roomState == RoomState.CIRCLE) {
            GSDataProxy.i.sendCircleOpen();
        }
        game.roomClean();
    }

    show(obj: any): void {
        if (!obj) return;

        this.data = obj;

        let layer = gameManager.layerManager.mainLayer;
        layer.addChild(this);

        this.lab_roomid.text = '房间号:' + game.roomid;

        this.btn_assess.visible = true;
        this.lab_rule.text = "" + game.ruleTexts();

        this.itemGroup.removeChildren();

        this.fenList = [];
        this.roleList = [];

        let persons: Array<any> = [];//数组 四个人
        for (let key in obj.persons) {
            persons.push(obj.persons[key]);
        }
        persons.sort(function (a, b) {
            if (+a.pos > +b.pos) return 1;
            else return -1;
        });

        let paolist: Array<number> = [];
        let settlement = obj.settlement;//json  四个分


        for (let k = 0; k < persons.length; k++) {
            let person = persons[k];
            let pos = person.pos;
            let cur: number = +settlement[pos];
            let pao_num: number = +person["pao_num"];

            person["iswin"] = this.getMaxNum(settlement, cur);
            person["cur"] = cur;

            let head: FinishPlayerItem;
            if (this.roleList[k]) {
                head = this.roleList[k];
                head.player = person;
            }
            else {
                head = new FinishPlayerItem(person);
                this.roleList.push(head);
            }

            paolist.push(pao_num);
            this.fenList.push(person);
        }


        for (let pi = 0; pi < this.roleList.length; pi++) {
            let item: FinishPlayerItem = this.roleList[pi];

            if (item.player["pao_num"] > 0) {
                item.player["ispao"] = true;
            }

            for (let k = 0; k < paolist.length; k++) {
                let kn: number = paolist[k];

                if (item.player["pao_num"] < kn) {
                    item.player["ispao"] = false;
                }
            }
        }

        for (let c = 0; c < this.roleList.length; c++) {
            let cc: FinishPlayerItem = this.roleList[c];
            this.itemGroup.addChild(cc);
            cc.refresh();
        }

        this.lab_time.text = "" + StringUtils.getYTDByTimestamp(Date.now()) + " " + StringUtils.getHMSByTimestamp(Date.now());

        Weixin.shareScore(this.fenList);

        GSController.i.scene.btnGroup.visible = false;
    }

    getMaxNum(arr: any, fen: number) {
        let b: boolean = true;

        if (fen <= 0) {
            return false;
        }

        for (let k in arr) {
            let num: number = +arr[k];

            if (fen < num) {
                b = false;
                break;
            }
        }

        return b;
    }
}